package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
	//获取当前登录用户的用户名
	@RequestMapping("/getUsername")
	public Result getUsername()throws Exception{
		try{
			org.springframework.security.core.userdetails.User user =
					(org.springframework.security.core.userdetails.User)
							SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());
		}catch (Exception e){
			return new Result(false, MessageConstant.GET_USERNAME_FAIL);

		}

	}
}
